# -*- coding: utf-8 -*-

"""
@Datetime: 2019/4/14
@Author: Zhang Yafei
"""
# 方式一 pandas模块
# import pandas as pd
#
# # 读取
# df = pd.read_csv('all_forum_info.csv')
# print(df.info())
#
# # 写入
# df.to_csv('data.csv')
# 参照我的博客 https://www.cnblogs.com/zhangyafei/p/10685438.html

# 方式二 csv模块
import csv

# 读取
# with open('all_forum_info.csv', encoding='utf-8') as myFile:
#     lines = csv.reader(myFile)
#     for line in lines:
#         print(line)
"""
写入
'w'表示写模式。
首先open()函数打开当前路径下的名字为't.csv'的文件，如果不存在这个文件，则创建它，返回myFile文件对象。
csv.writer(myFile)返回writer对象myWriter。
writerow()方法是一行一行写入，writerows方法是一次写入多行。
注意：如果文件't.csv'事先存在，调用writer函数会先清空原文件中的文本，再执行writerow/writerows方法。
"""

with open('data.csv', 'w', encoding='utf-8', newline='') as myFile:
    myWriter = csv.writer(myFile)
    myWriter.writerow(['title', '数学'])
    myWriter.writerow([8, 'h'])
    myList = [[1, 2, 3], [4, 5, 6]]
    myWriter.writerows(myList)

# 修改分割符
# csv.register_dialect('mydialect', delimiter='|', quoting=csv.QUOTE_ALL)
# with open('test.csv', 'r') as myFile:
#     lines = csv.reader(myFile, 'mydialect')
#     # print(lines.line_num)
#     for line in lines:
#         print(line)


